package com.tgq.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.tgq.demo.common.Constants;
import com.tgq.demo.common.Result;
import com.tgq.demo.dto.UserDto;
import com.tgq.demo.entity.User;
import com.tgq.demo.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/login")
@Tag(name = "login-controller",description = "登录相关")
public class LoginController {

    @Resource
    private IUserService userService;   //导service包
    //登录接口
    @PostMapping
    @ApiOperation("登录接口")
    public Result login(@RequestBody UserDto userDto){
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        if(StrUtil.isBlank(username)||StrUtil.isBlank(password)){
            return Result.error(Constants.CODR_400,"参数错误");
        }
        UserDto dto = userService.login(userDto);
        return Result.success(dto);
    }

    @PostMapping("register")
    @ApiOperation("注册")
    public Result register(@RequestBody UserDto userDto) {
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        if(StrUtil.isBlank(username)||StrUtil.isBlank(password)){
            return Result.error(Constants.CODR_400,"参数错误");
        }
        return Result.success(userService.register(userDto));
    }

}
